<template>
  <!-- 补充资料弹框 -->
  <el-dialog class="addtional_dialog"
  :visible.sync="visible_addtional" 
  title="补充资料" 
  @close="cancel"
  :close-on-click-modal="false" 
  :close-on-press-escape="false"
  :append-to-body="true"
  style="overflow: hidden;"
  >
  <!-- 上传相关附件或指定团员推优人 -->
  <el-form label-width="120px" label-position="right" class="tuanyuan" v-if="start=='团员'">
      <el-form-item label="指定团员推优人">
          <el-select clearable v-model="referrermentid">
              <el-option v-for="item in referrer" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
      </el-form-item>
      <!-- 上传附件 -->
      <div class="annex">
        <el-upload class="annex_uploadBorder" action="#"
        :on-change="uploadAnnex" drag 
        ref="tuanyuanapp" multiple
        accept=".png" :auto-upload="false" >
            <i class="el-icon-upload"></i>
            <p style="font-weight: 700;color:#f00;">上传会议附件</p>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传png文件，且不超过500kb</div>
        </el-upload>
      </div>
  </el-form>
  <el-form label-position="right" v-if="start=='入党积极分子'">
        <h3>指定两名入党介绍人：</h3>
        <el-form-item label="选择入党介绍人1：">
            <el-select clearable v-model="introducer1">
                <el-option v-for="item in referrer" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="选择入党介绍人2：">
            <el-select clearable v-model="introducer2">
                <el-option v-for="item in referrer" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="入党积极分子考察意见：">
            <el-input v-model="saveOpinionDto.opinion"
            :rows="6" placeholder="请输入内容" type="textarea"
            show-word-limit></el-input>
        </el-form-item>
  </el-form>
  <el-form label-width="120px" label-position="right" class="duixiang" v-if="start=='发展对象'">
      <!-- <span>会议信息</span> -->
      <el-form-item label="会议名称：">
          <el-input class="saveConferenceDtoinput" v-model="saveConferenceDto.name"></el-input>
      </el-form-item>
      <el-form-item label="会议人数：">
          <el-input class="saveConferenceDtoinput" v-model="saveConferenceDto.count"></el-input>
      </el-form-item>
      <el-form-item label="会议时间：">
          <!-- <el-input v-model="saveConferenceDto.count"></el-input> -->
          <div class="block">
            <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="saveConferenceDto.time"
            type="datetime"
            placeholder="选择日期">
            </el-date-picker>
          </div>
      </el-form-item>
      <el-form-item label="赞成人数：">
          <el-input class="saveConferenceDtoinput" v-model="saveConferenceDto.decideList[0].approveNumber"></el-input>
      </el-form-item>
      <el-form-item label="反对人数：">
          <el-input class="saveConferenceDtoinput" v-model="saveConferenceDto.decideList[0].opposeNumber"></el-input>
      </el-form-item>
    <el-form-item label="培养人意见：">
        <el-input class="saveConferenceDtoTextarea" type="textarea"
        :rows="5"
        placeholder="请输入内容"
        v-model="cultivate"
        show-word-limit></el-input>
    </el-form-item>
    <el-form-item label="党和群众意见：">
        <el-input class="saveConferenceDtoTextarea" type="textarea"
        placeholder="请输入内容"
        :rows="5"
        v-model="partyMessesOpinion"
        show-word-limit></el-input>
    </el-form-item>
  </el-form>
  <el-form class="prepare" v-if="start=='预备党员'">
      <span class="jilu">会议记录：</span>
      <el-row>
          <el-col :span="12">
              <el-form-item label="会议名称：">
                <el-input class="saveConferenceDtoinput" v-model="saveConferenceDto.name"></el-input>
              </el-form-item>
              <el-form-item label="会议时间：">
                <div class="block">
                    <el-date-picker
                    value-format="yyyy-MM-dd HH:mm:ss"
                    v-model="saveConferenceDto.time"
                    type="datetime"
                    placeholder="选择日期">
                    </el-date-picker>
                </div>
              </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="会议人数：">
                <el-input class="saveConferenceDtoinput" v-model="saveConferenceDto.count"></el-input>
              </el-form-item>
              <el-form-item label="赞成人数：">
                <el-input class="saveConferenceDtoinput" v-model="saveConferenceDto.decideList[0].approveNumber"></el-input>
              </el-form-item>
              <el-form-item label="反对人数：">
                <el-input class="saveConferenceDtoinput" v-model="saveConferenceDto.decideList[0].opposeNumber"></el-input>
              </el-form-item>
          </el-col>
      </el-row>
      <el-form-item label="党和群众意见：">
        <el-input class="saveConferenceDtoTextarea" type="textarea"
        placeholder="请输入内容"
        :rows="5"
        v-model="partyMessesOpinion"
        show-word-limit></el-input>
      </el-form-item>
      <span class="jilu">谈话内容：</span>
      <el-row >
          <!-- 问 -->
          <el-col :span="12">
              <el-form-item v-for="(item,index) in problem" :key="index"  :label="item.question">
                    <el-input style="width:300px;" v-model="item.questionLable" placeholder="输入问题"></el-input>
              </el-form-item>
          </el-col>
          <!-- 答 -->
          <el-col :span="12">
              <el-form-item  v-for="(item,index) in answer" :key="index"  :label="item.solution">
                  <el-input style="width:300px;" v-model="item.solutionLable" placeholder="回答"></el-input>
              </el-form-item>
          </el-col>
          <div style="text-align: right;padding-right:3vw;">
              <el-button type="primary" @click="addsession">增加对话框</el-button>
          </div>
      </el-row>
  </el-form>
  <div class="addtional_btn">
      <el-button type="primary" @click="subref">提交</el-button>
      <el-button @click="cancel">取消</el-button>
  </div>
  </el-dialog>
</template>

<script>
export default {
    data(){
        return{
            visible_addtional:false,
            start:"",//申请人当前阶段
            userid:"",//申请人id
            applicationId:"",//申请流程id
            referrermentid:"",//入党推优人id
            fileImg:"",//上传附件信息
            referrer:[],//党员list
            introducer1:"",//入党介绍人1
            introducer2:"",//入党介绍人2
            cultivate:"",//培养人意见
            partyMessesOpinion:"",//党和群众意见
            saveConferenceDto:{//会议信息
                count:"",//会议人数
                name:"",//会议名称
                time:"",//会议时间
                decideList:[
                    {//会议讨论人员
                        approveNumber:"",//赞成人数
                        opposeNumber:"",//反对人数
                        userId:"",//发起申请的用户的id
                    }
                ]
            },
            saveOpinionDto:{//入党积极分子考察意见
                opinion:"",
                userId:"",
            },
            problem:[],
            answer:[],
            str:"",//拼接问答字符串
        }
    },
    methods:{
        init(start,userid,applicationId){
            this.start = start,
            this.userid = userid,
            this.applicationId = applicationId
            this.visible_addtional = true
            this.problem.push({//问题
                question:"问",
                questionLable:"",
            })
            this.answer.push({//回答
                solution:"答",
                solutionLable:"",
            })
            this.axios({
                headers:{'Authentication':localStorage.getItem('token')},
                method:"get",
                url:`/user/getUserListInfo?politics=党员`,
            }).then(res=>{
                if(res.data.code != 20001){
                    return this.$message.error(res.data.msg)
                }
                this.referrer = res.data.data.list
                // console.log(this.referrer)
            })
        },
        subref(){//点击提交按钮
            if(this.start=="团员"){
                // this.userid this.referrermentid this.fileImg
                var annexImg = new FormData()
                annexImg.append("attachment",this.fileImg.raw)
                annexImg.append("referrerId",this.referrermentid)
                annexImg.append("userId",this.userid)
                this.axios({
                    headers:{'Authentication':localStorage.getItem('token')},
                    method:"post",
                    url:`/application/adminMemberApplicationResource`,
                    data:annexImg,
                }).then(res=>{
                    if(res.data.code != 20001){
                        return this.$message.error(res.data.msg)
                    }
                    // console.log(res)
                    this.$alert('提交成功!',{
                        confirmButtonText:'确定',
                        callback:action=>{
                            this.visible_addtional = false
                            this.referrermentid=""
                            this.$refs.tuanyuanapp.clearFiles()
                        }
                    })
                })
            }
            if(this.start=="入党积极分子"){
                this.saveOpinionDto.userId = this.userid
                Promise.all([
                    new Promise((resolve,reject)=>{
                        this.axios({
                            headers:{'Authentication':localStorage.getItem('token')},
                            method:"post",
                            url:`/application/adminActivityApplicationResource`,
                            data:{
                                introducerOneId:this.introducer1,
                                introducerTwoId:this.introducer2,
                                userId:this.userid
                            },
                        }).then(res=>{
                            if(res.data.code != 20001){
                                return this.$message.error(res.data.msg)
                            }
                            resolve(1)
                        })
                    }),
                    new Promise((resolve,reject)=>{
                        // 上传考察意见
                        this.axios({
                            headers:{'Authentication':localStorage.getItem('token')},
                            method:"post",
                            url:`/application/saveActivityInspectInfo`,
                            data:this.saveOpinionDto
                        }).then(res=>{
                            // console.log(res)
                            if(res.data.code != 20001){
                                return this.$message.error(res.data.msg)
                            }
                            resolve(1)//请求成功返回1
                        })
                    })
                ]).then(res=>{
                    if(res[0]==1&&res[1]==1){
                        this.$alert('提交成功!',{
                            confirmButtonText:'确定',
                            callback:action=>{
                                this.visible_addtional = false
                                this.introducer1 = ""
                                this.introducer2 = ""
                                this.saveOpinionDto = {
                                    opinion:"",
                                    userId:"",
                                }
                            }
                        })
                    }
                })
            }
            if(this.start=="发展对象"){
                this.saveConferenceDto.decideList[0].userId = this.userid
                Promise.all([//使用Promise统一处理获取的请求数据
                    new Promise((resolve,reject)=>{
                        this.axios({//请求两个人的意见接口
                            headers:{'Authentication':localStorage.getItem('token')},
                            method:"post",
                            url:`/application/adminDevelopApplicationResource`,
                            data:{
                                cultivateOpinion:this.cultivate,
                                partyMessesOpinion:this.partyMessesOpinion,
                                userId:this.userid
                            },
                        }).then(res=>{
                            if(res.data.code != 20001){
                                return this.$message.error(res.data.msg)
                            }
                            resolve(1)
                        })
                    }),
                    new Promise((resolve,reject)=>{
                        this.axios({
                            headers:{'Authentication':localStorage.getItem('token')},
                            method:"post",
                            url:`/application/saveConference`,
                            data:this.saveConferenceDto
                        }).then(res=>{
                            // console.log(res)
                            if(res.data.code != 20001){
                                return this.$message.error(res.data.msg)
                            }
                            resolve(1)
                        })
                    })
                ]).then(res=>{
                    if(res[0]==1&&res[1]==1){
                        this.$alert('提交成功!',{
                            confirmButtonText:'确定',
                            callback:action=>{
                                this.visible_addtional = false
                                this.cultivate = ""
                                this.partyMessesOpinion = ""
                                this.saveConferenceDto = {
                                    count:"",//会议人数
                                    name:"",//会议名称
                                    time:"",//会议时间
                                    decideList:[
                                        {//会议讨论人员
                                            approveNumber:"",//赞成人数
                                            opposeNumber:"",//反对人数
                                            userId:"",//发起申请的用户的id
                                        }
                                    ]
                                }
                            }
                        })
                    }
                })
            }
            if(this.start=="预备党员"){
                this.saveConferenceDto.decideList[0].userId = this.userid
                // 拼接谈话内容为字符串
                for(var i=0;i<this.problem.length;i++){
                    this.str+=this.problem[i].question+":"+this.problem[i].questionLable+this.answer[i].solution+":"+this.answer[i].solutionLable
                }
                // console.log(this.str)
                // console.log(this.partyMessesOpinion)
                Promise.all([
                    new Promise((resolve,reject)=>{
                        // 添加会议信息
                        this.axios({
                            headers:{'Authentication':localStorage.getItem('token')},
                            method:"post",
                            url:`/application/saveConference`,
                            data:this.saveConferenceDto
                        }).then(res=>{
                            // console.log(res)
                            if(res.data.code != 20001){
                                return this.$message.error(res.data.msg)
                            }
                            resolve(1)
                        })
                    }),
                    // 添加谈话内容以及党和群众意见
                    new Promise((resolve,reject)=>{
                        this.axios({
                            headers:{'Authentication':localStorage.getItem('token')},
                            method:"post",
                            url:`/application/adminReadyApplicationResource`,
                            data:{
                               conversion: this.str,
                               partyMessesOpinion:this.partyMessesOpinion,
                               userId:this.userid
                            }
                        }).then(res=>{
                            if(res.data.code != 20001){
                                return this.$message.error(res.data.msg)
                            }
                            resolve(1)
                        })
                    })
                ]).then(res=>{
                    if(res[0]==1&&res[1]==1){
                        this.$alert('提交成功!',{
                            confirmButtonText:'确定',
                            callback:action=>{
                                this.visible_addtional = false
                                this.partyMessesOpinion = ""
                                this.problem = []
                                this.answer = []
                                this.str = ""
                            }
                        })
                    }
                })
            }
        },
        uploadAnnex(file,fileList){
            this.fileImg = file
        },
        addsession(){//点击增加对话框
            this.problem.push({//问题
                question:"问",
                questionLable:"",
            })
            this.answer.push({//回答
                solution:"答",
                solutionLable:"",
            })
        },
        cancel(){
            this.visible_addtional = false
            if(this.start=="团员"){
                this.$refs.tuanyuanapp.clearFiles()
            }
            if(this.start=="入党积极分子"){
                this.introducer1 = ""
                this.introducer2 = ""
            }
            if(this.start=="发展对象"){
                this.cultivate = ""
                this.partyMessesOpinion = ""
                this.saveConferenceDto = {
                    count:"",//会议人数
                    name:"",//会议名称
                    time:"",//会议时间
                    decideList:[
                        {//会议讨论人员
                            approveNumber:"",//赞成人数
                            opposeNumber:"",//反对人数
                            userId:"",//发起申请的用户的id
                        }
                    ]
                }
            }
            if(this.start=="预备党员"){
                this.problem = []
                this.answer = []
                this.saveConferenceDto = {
                    count:"",//会议人数
                    name:"",//会议名称
                    time:"",//会议时间
                    decideList:[
                        {//会议讨论人员
                            approveNumber:"",//赞成人数
                            opposeNumber:"",//反对人数
                            userId:"",//发起申请的用户的id
                        }
                    ]
                }
                this.partyMessesOpinion = ""
            }
        }
    }
}
</script>

<style lang="scss">
    .addtional_dialog{
        .el-dialog{
            height: 78vh;
        }
        .tuanyuan{
            .annex{
                // border: 1px solid #000;
                .annex_uploadBorder{
                    text-align: center;
                    .el-upload-dragger{
                        height: 240px;
                        // border: 1px solid #000;
                    }
                }
            }
        }
        .duixiang{
            height: 55vh;
            overflow: auto;
            // border: 1px solid #000;
            .saveConferenceDtoinput{
                width: 220px;
            }
            .saveConferenceDtoTextarea{
                width: 500px;
            }
        }
        .prepare{
            height: 55vh;
            overflow: auto;
            // border: 1px solid #000;
            .jilu{
                margin-bottom:2vh;
                font-weight: 700;
                display: inline-block;
            }
            .saveConferenceDtoinput{
                width: 220px;
            }
            .saveConferenceDtoTextarea{
                width: 500px;
            }
        }
        .addtional_btn{
            position: absolute;
            bottom: 5vh;
            left: 0;
            width: 100%;
            text-align: center;
        }
    }
</style>